package second.graph;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class Solution01 {
    List<List<Integer>> res = new ArrayList<>();

    List<Integer> tmp = new LinkedList<>();

    public List<List<Integer>> allPathsSourceTarget(int[][] graph) {
        tmp.add(0);
        dfs(graph, 0);
        return res;
    }

    public void dfs(int[][] graph, int index) {
        if (index == graph.length - 1) {
            res.add(new ArrayList<>(tmp));
            return;
        }
        for (int i = 0; i < graph[index].length; i++) {
            tmp.add(graph[index][i]);
            dfs(graph, graph[index][i]);
            tmp.remove(tmp.size() - 1);
        }
    }
}
